package com.cqeec.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
 
import com.cqeec.domain.Student; 
import com.cqeec.service.StudentService; 
import com.cqeec.service.imp.StudentServiceImpl;
import com.cqeec.utils.JDBCUtils;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
/**
 * 添加学生
 * @author Administrator
 *
 */
public class AddStudents extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private Connection conn;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddStudents frame = new AddStudents();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddStudents() {
		setIconifiable(true);
		setClosable(true);
		setTitle("添加学生");
		setBounds(100, 100, 553, 553);
		
		JLabel lblNewLabel = new JLabel("学生姓名：");
		
		JLabel lblNewLabel_1 = new JLabel("学生学号：");
		
		JLabel lblNewLabel_2 = new JLabel("登录密码：");
		
		JLabel lblNewLabel_3 = new JLabel("院系：");
		
		textField = new JTextField();
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			

			
			/**
			 * 添加学生事件
			 */
			public void actionPerformed(ActionEvent e) {
				
				
				
				conn=JDBCUtils.getConnection();
			String studentName=textField.getText(); //获取学生姓名
			String studentNum=textField_1.getText();//获取学生学号
			String studentPwd=textField_2.getText();//获取学生登录密码
			String studentMajor=textField_3.getText();//获取学生院系
			
			if(studentName.equals("")) {
				JOptionPane.showMessageDialog(null, "请输入学生姓名", "提示", JOptionPane.ERROR_MESSAGE);
				textField.requestFocus();
				return;
			}
			
			if(studentNum.equals("")) {
				JOptionPane.showMessageDialog(null, "请输入学生学号", "提示", JOptionPane.ERROR_MESSAGE);
				textField_1.requestFocus();
				return;
			} 

			if(studentPwd.equals("")) {
				JOptionPane.showMessageDialog(null, "请输入学生密码", "提示", JOptionPane.ERROR_MESSAGE);
				textField_2.requestFocus();
				return;
			}

			if(studentMajor.equals("")) {
				JOptionPane.showMessageDialog(null, "请输入学生院系", "提示", JOptionPane.ERROR_MESSAGE);
				textField_3.requestFocus();
				return;
			}
          Student student=new Student(studentName,studentNum,studentPwd,studentMajor);
          StudentService dao=new StudentServiceImpl();
            int rs=dao.addStudents(conn, student);
            if(rs!=0) {
            	JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.ERROR_MESSAGE);
            }
			
				
			}
		});
		
		JButton button_1 = new JButton("重置");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(83)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_3)
									.addGap(26)
									.addComponent(textField_3))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_2)
									.addGap(18)
									.addComponent(textField_2))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField_1))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(textField, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(126)
							.addComponent(button)
							.addGap(91)
							.addComponent(button_1)))
					.addContainerGap(343, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(103)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(65)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(65)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(46)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addGap(36))
		);
		getContentPane().setLayout(groupLayout);

	}
}
